from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from datetime import datetime

db = SQLAlchemy()

# 商品分类模型
class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False, unique=True)  # 分类名称（如电子产品、家居）
    slug = db.Column(db.String(50), nullable=False, unique=True)  # URL友好标识
    create_time = db.Column(db.DateTime, default=datetime.now)

    # 关联商品（一对多）
    products = db.relationship('Product', backref='category', lazy=True)


# 商品模型（核心，含库存字段）
class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)  # 商品名称
    description = db.Column(db.Text, nullable=True)   # 商品描述
    price = db.Column(db.Float, nullable=False)       # 商品价格
    stock = db.Column(db.Integer, nullable=False, default=0)  # 库存数量（核心字段）
    image_url = db.Column(db.String(200), nullable=True)  # 商品图片URL
    rating = db.Column(db.Float, default=4.5)         # 商品评分（用于排序）
    review_count = db.Column(db.Integer, default=0)   # 评价数量
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False)  # 关联分类
    create_time = db.Column(db.DateTime, default=datetime.now)

    # 关联购物车项
    cart_items = db.relationship('CartItem', backref='product', lazy=True)
    
# 购物车模型（按session_id区分用户，无需登录）
class Cart(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    session_id = db.Column(db.String(100), nullable=False, unique=True)  # 前端生成的会话ID
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

    # 关联购物车项
    items = db.relationship('CartItem', backref='cart', lazy=True, cascade='all, delete-orphan')

# 购物车项模型（关联购物车和商品）
class CartItem(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    cart_id = db.Column(db.Integer, db.ForeignKey('cart.id'), nullable=False)
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False)
    quantity = db.Column(db.Integer, nullable=False, default=1)  # 商品数量
    create_time = db.Column(db.DateTime, default=datetime.now)

    # 避免重复添加同一商品
    __table_args__ = (db.UniqueConstraint('cart_id', 'product_id', name='_cart_product_unique'),)


class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(120), nullable=False)


# class Order(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
#     total_amount = db.Column(db.Float, nullable=False)
#     status = db.Column(db.String(20), default='paid')
#     created_at = db.Column(db.DateTime, default=datetime.now)
#     items = db.relationship('OrderItem', backref='order', lazy=True)

class Orders(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

class OrderItem(db.Model):
    id = db.Column(db.Integer, primary_key=True)
#     order_id = db.Column(db.Integer, db.ForeignKey('order.id'), nullable=False)
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False)
#     quantity = db.Column(db.Integer, nullable=False)
#     price = db.Column(db.Float, nullable=False)
#     product = db.relationship('Product')    